package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.business.domain.*;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class AccountFlowServiceImpl implements IAccountFlowService {
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    public void createBaseFlow(Account account,BigDecimal amount,String note,Integer actionType){
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setActionType(actionType);
        accountFlow.setAmount(amount);
        accountFlow.setNote(note);
        accountFlow.setUsableAmount(account.getUsableAmount());
        accountFlow.setFreezedAmount(account.getFreezedAmount());
        accountFlow.setActionTime(new Date());
        accountFlow.setAccountId(account.getId());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void createRechargeOfflineSuccessFlow(RechargeOffline rechargeOffline, Account account) {
        String note = "线下成功充值["+rechargeOffline.getAmount()+"]";
        createBaseFlow(account,rechargeOffline.getAmount(),note,Constant.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
    }

    @Override
    public void createFreezedSuccessFlow(String title,Account account, BigDecimal amount) {
        String note = "投标["+title+"]冻结,可用余额减少,冻结增加["+amount+"]元";
        createBaseFlow(account,amount,note,Constant.ACCOUNT_ACTIONTYPE_BID_FREEZED);

    }

    @Override
    public void createBidFailFlow(Account account, Bid bid) {
        String note = "投标[" + bid.getBidRequestTitle() + "]失败,冻结减少,可用余额增加[" + bid.getAvailableAmount()+"]元";
        createBaseFlow(account,bid.getAvailableAmount(),note,Constant.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
    }

    @Override
    public void createBorrowerSuccessFlow(Account account, BidRequest bidRequest) {
        BigDecimal bidRequestAmount = bidRequest.getBidRequestAmount();
        bidRequest.getBidRequestAmount();
        String note = "借款成功,可用余额增加[" + bidRequestAmount+"]元";
        createBaseFlow(account,bidRequestAmount,note,Constant.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL);
    }

    @Override
    public void createPaymentManagementFlow(BigDecimal managementCharge,Account account,String  bidRequestTitle) {
        String note = "["+bidRequestTitle+"]标"+"借款成功,支付平台管理费["+managementCharge+"]元";
        createBaseFlow(account,managementCharge,note,Constant.ACCOUNT_ACTIONTYPE_CHARGE);
    }

    @Override
    public void createBorrowerSuccessFreezedFlow(Account account, BigDecimal investmentAmount,String bidRequestTitle) {
        String note = "投标["+ bidRequestTitle + "]标成功,冻结资金减少[" + investmentAmount +"]元";
        createBaseFlow(account,investmentAmount,note,Constant.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
    }

    @Override
    public void createRepaymentSuccessFlow(Account account, String title,BigDecimal totalAmount) {
        String note = "[" + title+"]标还款成功,可用余额减少[" +totalAmount+"]元";
        createBaseFlow(account,totalAmount,note,Constant.ACCOUNT_ACTIONTYPE_RETURN_MONEY);
    }

    @Override
    public void createPayLateFeeFlow(Account account, BigDecimal latePaymentAmount, String bidRequestTitle,int monthIndex,int days) {
        String note = "支付[" + bidRequestTitle +"]标第["+monthIndex+"]期逾期["+days+"]天滞纳金[" + latePaymentAmount + "]元";
        createBaseFlow(account,latePaymentAmount,note,Constant.ACCOUNT_PAY_LATE_PAYMENT_FEE);
    }

    @Override
    public void createReceiveLatePaymentAmountFlow(Account account, BigDecimal latePaymentAmount, String bidRequestTitle,int monthIndex,int days) {
        String note = "收取[" + bidRequestTitle +"]标第["+monthIndex+"]期逾期["+days+"]天的滞纳金[" + latePaymentAmount + "]元";
        createBaseFlow(account,latePaymentAmount,note,Constant.ACCOUNT_RECEIVE_PAY_LATE_PAYMENT_FEE);
    }

    @Override
    public void createReceiveCreditTransferFlow(Account account, BigDecimal amount, String bidRequestTitle,int remainMonthIndex) {
        String note = "收取[" + bidRequestTitle +"]标共["+remainMonthIndex+"]期债权转让本金[" + amount + "]元";
        createBaseFlow(account,amount,note,Constant.TRANSFER_OF_CREDITOR_RIGHTS);
    }

    @Override
    public void createSubscribeSuccess(Account account, BigDecimal amount, String bidRequestTitle,int remainMonthIndex) {
        String note = "认购[" + bidRequestTitle +"]标共["+remainMonthIndex+"]期债权转让本金[" + amount + "]元";
        createBaseFlow(account,amount,note,Constant.SUBSCRIPTION_CREDIT);
    }

    @Override
    public void creatReceivePaymentExperienceInterest(Account account, BigDecimal amount, String bidRequestTitle) {
        String note = "收到[" + bidRequestTitle +"]体验标利息,可用余额增加[" + amount + "]元";
        createBaseFlow(account,amount,note,Constant.RECEIVE_PAYMENT_EXPERIENCE_INTEREST);
    }


    @Override
    public void createBiddingReturnFlow(Account account,String title,BigDecimal amount) {
        String note = "收到[" + title +"]标还款,可用余额增加" + amount + "]元";
        createBaseFlow(account,amount,note,Constant.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY);

    }

    @Override
    public void createInterestManagerChargeFlow(Account account,String title,BigDecimal amount) {
        String note = "收到[" + title +"]标利息,支付平台利息管理费[" + amount + "]元";
        createBaseFlow(account,amount,note,Constant.ACCOUNT_ACTIONTYPE_INTEREST_SHARE);
    }

}
